package com.kzd.sdk.ui.fragment;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.entity.KZD_GameMove;
import com.kzd.sdk.ui.activity.KZD_WebRichActivity;
import com.kzd.sdk.ui.adapter.KZD_GameMoveAdapter;
import com.kzd.sdk.utils.AppUtils;

import java.util.List;

public class KZD_HomeMoveFragment extends OwnerFragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private KZD_GameMoveAdapter mGameMoveAdapter;

    private KZD_HttpCallback<List<KZD_GameMove>> callbackGameMove = new KZD_HttpCallback<List<KZD_GameMove>>() {
        @Override
        public void onSuccess(List<KZD_GameMove> data) {
            mGameMoveAdapter.setNewInstance(data);
            mSwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_include_app_recycler";
    }

    @Override
    public void initView(View view) {
        mRecyclerView = view.findViewById(AppUtils.getId(mContext, "mRecyclerView"));
        mGameMoveAdapter = new KZD_GameMoveAdapter(mContext);
        mGameMoveAdapter.setOnItemClickListener(new KZD_OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                KZD_GameMove itemData = mGameMoveAdapter.get(position);
                KZD_WebRichActivity.openCon(itemData.getTitle(), itemData.getContent());
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mGameMoveAdapter);

        mSwipeRefreshLayout = view.findViewById(AppUtils.getId(mContext, "mSwipeRefreshLayout"));
        mSwipeRefreshLayout.setColorSchemeResources(AppUtils.getColorId(mContext, "kzd_2983ff"));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    @Override
    public void initData() {
        KZD_ApiManager.httpGameMove(callbackGameMove);
    }
}
